package com.randi.common.config;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author ziye
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {

    private String name = "App";

    private String version = "1.0.0";

    private String copyrightYear = "2024";

    @Resource
    private SystemConfig system;

    @Resource
    private TokenConfig token;

    @Resource
    private FileConfig file;

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "app.system")
    public static class SystemConfig {
        private String superAdmin = "admin";
        private Long userAdminId = 1L;
        private Long roleAdminId = 1L;
        private String logoutUrl = "/logout";

        @Resource
        private PasswordConfig password;

        @Resource
        private SecretConfig secret;

        @Data
        @Configuration
        public static class PasswordConfig {
            /**
             * 默认密码
             */
            private String value = "$10$qaf2bnMDwWHUoDyjsvHszeW.OHlIMRVy5zZhxdFhj9ZnnwUTxdWPC";
            /**
             * 密码最大错误次数
             */
            private Integer maxRetryCount = 5;
            /**
             * 密码锁定时间（默认10分钟）
             */
            private Integer lockTime = 10;
            /**
             * 更新最大错误次数
             */
            private Integer updateMaxRetryCount = 5;
            /**
             * 更新锁定时间
             */
            private Integer updateLockTime = 30;
            /**
             * 更新后重新登录
             */
            private Boolean updateAgainLogin = true;
        }

        @Data
        @Configuration
        public static class SecretConfig {
            /**
             * 加密盐值
             */
            private String salt = "63eeac68cf074c8c";
            /**
             * 密码过期时间（默认5分钟）
             */
            private Integer expireTime = 5;
        }

    }

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "app.token")
    public static class TokenConfig {
        private String key = "ziye";

        @Resource
        private AccessTokenConfig access;

        @Resource
        private RefreshTokenConfig refresh;

        @Data
        @Configuration
        public static class JwtTokenConfig {
            /**
             * 请求头标识
             */
            private String header = "Authorization";
            /**
             * 前端获取字段名
             */
            private String fieldName = "accessToken";
            /**
             * 过期时间（默认2小时）
             */
            private Integer expireTime = 7200;
        }

        @Configuration
        public static class AccessTokenConfig extends JwtTokenConfig {
        }

        @Configuration
        public static class RefreshTokenConfig extends JwtTokenConfig {
        }

    }

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "app.file")
    public static class FileConfig {
        private String path = "/static";

        @Resource
        private UploadFileConfig upload;

        @Data
        @Configuration
        public static class UploadFileConfig {
            /**
             * 最大上传文件大小 10MB
             */
            private Long maxSize = 10L * 1024 * 1024;
            /**
             * 保存路径后缀
             */
            private String savePathSuffix = "/upload";
            /**
             * 分片保存路径后缀
             */
            private String slicePathSuffix = "/slice_temp";
            /**
             * 分片自动清理
             */
            private Boolean sliceAutoClean = false;

        }
    }

}